package ltd.newbee.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ltd.newbee.mall.api.mall.vo.NewBeeMallIndexConfigGoodsVO;
import ltd.newbee.mall.entity.IndexConfig;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.mapper.IndexConfigMapper;
import ltd.newbee.mall.mapper.NewBeeMallGoodsMapper;
import ltd.newbee.mall.service.NewBeeMallIndexConfigService;
import ltd.newbee.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NewBeeMallIndexConfigServiceImpl implements NewBeeMallIndexConfigService {
    @Autowired
    private IndexConfigMapper indexConfigMapper;

    @Autowired
    private NewBeeMallGoodsMapper goodsMapper;

    @Override
    public List<NewBeeMallIndexConfigGoodsVO> getConfigGoodsForIndex(int configType, int number) {
        List<NewBeeMallIndexConfigGoodsVO> newBeeMallIndexConfigGoodsVOS = new ArrayList<>(number);

        LambdaQueryWrapper<IndexConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .eq(IndexConfig::getConfigType, configType)
                .eq(IndexConfig::getIsDeleted, 0)
                .orderByDesc(IndexConfig::getConfigRank);
        if (number > 0) {
            queryWrapper.last("limit " + number);
        }
        List<IndexConfig> indexConfigs = indexConfigMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(indexConfigs)) {
            List<Long> goodsIds = indexConfigs.stream().map(IndexConfig::getGoodsId).collect(Collectors.toList());
            LambdaQueryWrapper<NewBeeMallGoods> goodQueryWrapper = Wrappers.lambdaQuery();
            goodQueryWrapper.in(NewBeeMallGoods::getGoodsId, goodsIds).last("ORDER BY FIELD(goods_id, " + goodsIds.stream().map(String::valueOf).collect(Collectors.joining(",")) + ")");
            List<NewBeeMallGoods> newBeeMallGoods = goodsMapper.selectList(goodQueryWrapper);

            newBeeMallIndexConfigGoodsVOS = BeanUtil.copyList(newBeeMallGoods, NewBeeMallIndexConfigGoodsVO.class);
            for (NewBeeMallIndexConfigGoodsVO newBeeMallIndexConfigGoodsVO : newBeeMallIndexConfigGoodsVOS) {
                String goodsName = newBeeMallIndexConfigGoodsVO.getGoodsName();
                String goodsIntro = newBeeMallIndexConfigGoodsVO.getGoodsIntro();
                if (goodsName.length() > 30) {
                    goodsName = goodsName.substring(0, 30) + "...";
                    newBeeMallIndexConfigGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 22) {
                    goodsIntro = goodsIntro.substring(0, 22) + "...";
                    newBeeMallIndexConfigGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        return newBeeMallIndexConfigGoodsVOS;
    }
}
